package threaddemo;

public class demo8 {
    // wait()和notify()方法
    // 1. 调用wait()方法后，线程会进入等待状态，并且释放锁
    // 2. 调用notify()方法后，线程会从等待状态中唤醒，并且重新获取锁
    // 3. 调用notifyAll()方法后，所有等待的线程都会从等待状态中唤醒，并且重新获取锁
    public static void main(String[] args) {
        Object object = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (object) {
                System.out.println("Thread 1: Holding resource 1...");
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("Thread 1: Resource 1 released.");
            }
        });
        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            synchronized (object) {
                System.out.println("通知Thread 1");
                object.notify();
            }
        });
        t1.start();
        t2.start();
    }
}
